#define once

#include<iostream>
#include<string>
#include<fstream>
#include<unordered_map>

#include"Common.hpp"
#include"Log.hpp"
const std::string gpath="./";
const std::string gdictname="dictionary.txt";
const std::string gsep=": ";

using namespace LogModule;

class Dictionary
{
    private:
    bool LoadDictionary()
    {
        std::string file=_path+_dictname;
        std::ifstream in(file.c_str());//从文件中读取数据的输入文件流类
        if(!in.is_open())
        {
            LOG(LogLevel::ERROR)<<"open file"<<file<<"errno";
            return false;
        }
        std::string line;
        while (std::getline(in,line))//将输入流读取到的储存到line中
        {
           std::string key;
           std::string value;

           if(SplitString(line,&key,&value,gsep))
           {
                _dictionary.insert(std::make_pair(key,value));
           }
        }

        in.close();
        return true;


    }
    public:
    Dictionary(const std::string &path=gpath,const std::string &dictname=gdictname):_path(path),_dictname(dictname)
    {
        LoadDictionary();
        
    }
    std::string Translate(const std::string &word)
    {
        auto ite=_dictionary.find(word);
        if(ite==_dictionary.end()) return ".....";
        return ite->second;
    }
    ~Dictionary()
    {

    }
    private:
    std::unordered_map<std::string,std::string> _dictionary;
    std::string _path;
    std::string _dictname;
};


